/*
 * @Author: lixu lixu@puchigames.com
 * @Date: 2025-01-03 16:49:48
 * @LastEditors: lixu lixu@puchigames.com
 * @LastEditTime: 2025-09-16 17:59:04
 * @FilePath: /go-helper/ixRedis/utils.go
 * @Description: Redis 工具方法
 */
package ixRedis

import "github.com/garyburd/redigo/redis"

// Keys 获取匹配的键
func (c *Client) Keys(pattern string) ([]string, error) {
	conn, err := c.getConn()
	if err != nil {
		return nil, err
	}
	defer conn.Close()

	// 如果设置了前缀，需要在模式前加上前缀
	fullPattern := pattern
	if c.config.KeyPrefix != "" {
		fullPattern = c.config.KeyPrefix + ":" + pattern
	}

	return redis.Strings(conn.Do("KEYS", fullPattern))
}

// FlushDB 清空当前数据库
func (c *Client) FlushDB() error {
	conn, err := c.getConn()
	if err != nil {
		return err
	}
	defer conn.Close()

	_, err = conn.Do("FLUSHDB")
	return err
}

// FlushAll 清空所有数据库
func (c *Client) FlushAll() error {
	conn, err := c.getConn()
	if err != nil {
		return err
	}
	defer conn.Close()

	_, err = conn.Do("FLUSHALL")
	return err
}

// Info 获取服务器信息
func (c *Client) Info() (string, error) {
	conn, err := c.getConn()
	if err != nil {
		return "", err
	}
	defer conn.Close()

	return redis.String(conn.Do("INFO"))
}
